package com.example.album;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jakarta.servlet.http.HttpSession;

@Service
public class UserManagementService {
    @Autowired
    private UserRepository ur;
    @Autowired
    private AlbumRepository ar;
    @Autowired
    private LikeRepository lr;
    @Autowired
    private CollectionRepository cor;
    @Autowired
    private HttpSession session;

    // public boolean login(String userid, String password) {
    //     User user = ur.findByUserIdAndPassword(userid, password);
    //     return user != null && user.getStatus() == 0;
    // }

    public Map<String, Object> login(String userid, String password) {
        Map<String, Object> result = new HashMap<>();
        User user = ur.findByUserIdAndPassword(userid, password);
        if(user == null) {
            user = ur.findByPhoneNumberAndPassword(userid, password);
        }
        if (user != null && user.getStatus() == 0) {
            result.put("success", true);
            result.put("role", user.getRole());
            session.setAttribute("userid", user.getUserId());
        } else if (user == null) {
            result.put("success", false);
            result.put("message", "未找到用户或密码错误");
        } else {
            result.put("success", false);
            result.put("message", "用户被封禁或注销");
        }
        return result;
    }

    public String register(String username, String password) {
        long count = ur.count();
        String newUserId = String.valueOf(count + 1);
        while (ur.findByUserId(newUserId) != null) {
            newUserId = String.valueOf(Integer.parseInt(newUserId) + 1);
        }
        User newUser = new User();
        newUser.setUserId(newUserId);
        newUser.setPassword(password);
        newUser.setUsername(username);
        newUser.setRole("普通用户");
        newUser.setStatus(0);
        ur.save(newUser);
        return newUserId;
    }

    public User getUserById(String userId) {
        return ur.findByUserId(userId);
    }

    public void updateUserInfo(String userId, String username, String password, String phone) {
        User user = ur.findByUserId(userId);
        if (user != null) {
            if( username != null && !username.isEmpty())
            user.setUsername(username);
            if( password != null && !password.isEmpty())
            user.setPassword(password);
            if( phone != null && !phone.isEmpty())
            user.setPhoneNumber(phone);
            ur.save(user);
        }
    }

    public void cancelUser(String userId) {
        User user = ur.findByUserId(userId);
        if (user != null) {
            user.setStatus(1); // 假设1表示已注销
            ur.save(user);
        }
    }

    public Map<String, Object> getUserStats(String userId) {
        Map<String, Object> stats = new HashMap<>();
        long albumCount = ar.countAlbumsByOwnerIdAndStatusNot(userId, 1);
        // 新逻辑：统计当前用户所有相册的获赞数
        java.util.List<String> albumIds = ar.findAlbumIdsByOwnerIdAndStatusNot(userId, 1);
        long likeCount = 0;
        if (albumIds != null && !albumIds.isEmpty()) {
            likeCount = lr.countByAlbumIdIn(albumIds);
        }
        long favCount = cor.countCollectionsByUserId(userId);
        stats.put("albumCount", albumCount);
        stats.put("likeCount", likeCount);
        stats.put("favCount", favCount);
        return stats;
    }
    public Map<String, Object> getAllUsersList() {
        Map<String, Object> result = new HashMap<>();
        java.util.List<User> users = ur.findAll();
        java.util.List<Map<String, Object>> userList = new java.util.ArrayList<>();
        for (User user : users) {
            Map<String, Object> userMap = new HashMap<>();
            userMap.put("userId", user.getUserId());
            userMap.put("username", user.getUsername());
            userMap.put("role", user.getRole());
            userMap.put("status", user.getStatus());
            userList.add(userMap);
        }
        result.put("users", userList);
        return result;
    }
}
